package com.mbyte.easy.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.qcbyclass.entity.QcbyClass;
import com.mbyte.easy.qcbyclass.service.IQcbyClassService;
import com.mbyte.easy.sign.entity.Sign;
import com.mbyte.easy.sign.mapper.SignMapper;
import com.mbyte.easy.sign.service.ISignService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.student.entity.Student;
import com.mbyte.easy.student.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2020-06-16
 */
@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign> implements ISignService {

    @Autowired
    private IStudentService studentService;
    @Autowired
    private IQcbyClassService qcbyClassService;
    @Autowired
    private ISignService signService;

    //为list列表的sign加上姓名和班级名(wq2020.6.16)
    @Override
    public IPage<Sign> pageAddName(Page<Sign> page, QueryWrapper<Sign> queryWrapper) {
        IPage<Sign> page1 = page(page, queryWrapper);
        List<Sign> records = page1.getRecords();
        //获取class集合转化成map
        Map<Long, String> classMap = qcbyClassService.listNormal().stream().collect(Collectors.toMap(QcbyClass::getId, QcbyClass::getClassName));
        for (int i = 0; i<records.size();i++) {
            Student one = studentService.getOne(new LambdaQueryWrapper<Student>().eq(Student::getUserId, records.get(i).getUserId()));
            records.get(i).setName(one.getName());
            records.get(i).setClassName(classMap.get(records.get(i).getClassId()));
        }
        return page1;
    }
    //为修改页面的sign加上姓名和班级名(wq2020.6.17)
    @Override
    public Sign addNameAndClassName(Long id) {
        Sign sign = signService.getById(id);
        sign.setName(studentService.getOne(new LambdaQueryWrapper<Student>().eq(Student::getUserId, sign.getUserId())).getName());
        sign.setClassName(qcbyClassService.getOne(new LambdaQueryWrapper<QcbyClass>().eq(QcbyClass::getId,sign.getClassId())).getClassName());
        return sign;
    }
}
